<?php
include "helper.php";
include "graphics.php";
include "loadconfig.php";
include "database.php";

$startLoadingTime = time();
$cookieDate = time() + 31536000; // Cookies are keeped for one year

global $settings;
global $page;
global $defaultPage;

$defaultPage='home';

if (isset($_GET['page']))
    $page = $_GET['page'];
else
    $page = $defaultPage;

$settings = LoadConfig(); // Load some database settings from a txt file
$_SESSION['settings'] = $settings;
$title = "Sos Services";

$myCon = new Database($_SESSION['settings']['servername'], $_SESSION['settings']['username'], $_SESSION['settings']['password']); // Database object
$myCon->SelectDB($_SESSION['settings']['database']);
?>


<html>
<head>
<?php echo "<title>".$title."</title>\n";?>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<link rel='stylesheet' type='text/css' href='./css/generic_style.css'>
<link rel='stylesheet' type='text/css' href='./css/fivestars_style.css'>
<script type="text/javascript" src="./jscripts/protoype.js"></script>
<script type="text/javascript" src="./jscripts/jquery.js"></script>
<script type="text/javascript" src="./jscripts/script.js"></script>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body style='overflow:auto;'>
<?php

    function DisplayContent($contentPage)
    {
        global $myCon;
        
        $result = $myCon->Query("SELECT * FROM horizontal_menu LEFT JOIN content_page ON horizontal_menu.id=content_page.menuid JOIN page_type ON content_page.type=page_type.id WHERE horizontal_menu.item= '".$contentPage."' ORDER BY horizontal_menu.order_display");
        $row = mysql_fetch_object($result);
        if (mysql_num_rows($result))
        {  
            $oTable = new Table($row->item);
            $oTable->NewLine();
            $oTable->NewCol();
            
            if ($row->contentpage != NULL and $row->type == 'php')
                $oTable->Write(eval($row->contentpage));
            else
                $oTable->Write($row->contentpage);
            
            $oTable->CloseCol();
            $oTable->CloseLine();
            $oTable->CloseTable();
            echo $oTable->PrintTable();
        }
        else
        {
            echo 'No records';
        }
        mysql_free_result($result);
    }
    

    function DisplayRating($advertiser_id)
    {
        global $myCon;
        $result = $myCon->Query("SELECT * FROM fivestars WHERE advertiser_id=".$advertiser_id);
        $content = '';
        while($row = mysql_fetch_object($result))
        {
            $rating = (int)$row->rating;
            
                $content .= '<div class="floatleft">';
                $content .= '<div id="rating_';
                $content .= $row->id;
                $content .= '">';
                $content .= '<span class="star_1"><img src="./images/star_blank.png" alt="" '; if($rating > 0)   { $content .= 'class="hover"'; } $content .= '/></span>';
                $content .= '<span class="star_2"><img src="./images/star_blank.png" alt="" '; if($rating > 1.5) { $content .= 'class="hover"'; } $content .= '/></span>';
                $content .= '<span class="star_3"><img src="./images/star_blank.png" alt="" '; if($rating > 2.5) { $content .= 'class="hover"'; } $content .= '/></span>';
                $content .= '<span class="star_4"><img src="./images/star_blank.png" alt="" '; if($rating > 3.5) { $content .= 'class="hover"'; } $content .= '/></span>';
                $content .= '<span class="star_5"><img src="./images/star_blank.png" alt="" '; if($rating > 4.5) { $content .= 'class="hover"'; } $content .= '/></span>';
                $content .= '</div>';
        	$content .= '</div>';
        	$content .= '<div class="star_rating">';
            $content .= '(Rated <strong>';
            $content .= $rating;
            $content .= '</strong> Stars)';
        	$content .= '</div>';
        	$content .= '<div class="clearleft">&nbsp;</div>';
        }
        return $content;
    }
    
    echo "<div id='spacer'></div>";
    echo '<table class="page" align=center width=1200px>';
    echo '<tr valign=top>';
    echo '<td>';

    // -------------------------------------------------------------------------------------------
    // Spacer Header
    // -------------------------------------------------------------------------------------------
    
    // -------------------------------------------------------------------------------------------
    // Banner
    // -------------------------------------------------------------------------------------------
    
    // Query all advertiser from the database
    $result = $myCon->Query("SELECT * FROM banner");
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_object($result);
        $oTable = new Table('banner');
        $oTable->NewLine();
        $oTable->NewCol('logo');
        $oTable->NewImage('images/logo.jpg', 'bannerPic');
        $oTable->CloseCol();
        $oTable->NewCol();
        $oTable->Write($title);
        $oTable->CloseCol();
        $oTable->CloseLine();
        $oTable->CloseTable();
        echo $oTable->PrintTable();
        mysql_free_result($result);
        unset($oTable);
    }
    else
    {
        echo 'No banner set';
    }
    
    echo "<div class='spacer'></div>";
    
    // -------------------------------------------------------------------------------------------
    // Horizontal menu
    // -------------------------------------------------------------------------------------------
    
    echo "<div id='hMenu'>";
    // Query all advertiser from the database
    $result = $myCon->Query("SELECT * FROM horizontal_menu ORDER BY order_display");
    if (mysql_num_rows($result))
    {
        $oTable = new Table('horizontalMenu');
        $oTable->NewLine();
        while ($row = mysql_fetch_object($result))
        {
            $oTable->NewCol();
            $oTable->Hyperlink('hMenu', $row->item, $row->url);
            $oTable->CloseCol();
        }
        $oTable->CloseLine();   
        $oTable->CloseTable();
        echo $oTable->PrintTable();
        mysql_free_result($result);
        unset($oTable);
    }
    else
    {
        echo 'No horizontal menu set';
    }
    echo "</div>";
    
    // -------------------------------------------------------------------------------------------
    // Spacer
    // -------------------------------------------------------------------------------------------
    echo "<div class='spacer'></div>";

    // -------------------------------------------------------------------------------------------
    // Vertical menu
    // -------------------------------------------------------------------------------------------
    
    echo "<table class='container'>";
    echo "<tr valign=top>";
//    echo "<td>";
//    
//    echo "<div id='vMenu'>";
//        
//    // Query all advertiser from the database
//    $result = $myCon->Query("SELECT * FROM vertical_menu");
//    if (mysql_num_rows($result))
//    {
//        $row = mysql_fetch_object($result);
//        $oTable = new Table('verticalMenu');
//        $oTable->NewLine();
//        $oTable->NewCol();
//        $oTable->Write('Vertical Menu');
//        $oTable->CloseCol();
//        $oTable->CloseLine();
//        $oTable->CloseTable();
//        echo $oTable->PrintTable();
//        mysql_free_result($result);
//        unset($oTable);
//    }
//    else
//    {
//        echo 'No vertical menu set';
//    }
//    echo "</div>";
//    
//    echo "</td>";

    // -------------------------------------------------------------------------------------------
    // Records
    // -------------------------------------------------------------------------------------------
    
    echo "<td>\n";
    
    echo "<div id='advertiser'>\n";
   
    DisplayContent($page);   
    
    echo "</div>";
    echo "</td>";
    echo "</tr>";
    echo "</table>";
    
    // -------------------------------------------------------------------------------------------
    // Spacer
    // -------------------------------------------------------------------------------------------
    echo "<div class='spacer'></div>";
    
    // -------------------------------------------------------------------------------------------
    // Footer
    // -------------------------------------------------------------------------------------------
    
    echo "<div id='footer'>";
    
    // Query all advertiser from the database
    $result = $myCon->Query("SELECT * FROM footer");
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_object($result);
        $oTable = new Table('footer');
        $oTable->NewLine();
        $oTable->NewCol();
        $oTable->Write('Footer');
        $oTable->CloseCol();
        $oTable->CloseLine();
        $oTable->CloseTable();
        echo $oTable->PrintTable();
        mysql_free_result($result);
        unset($oTable);
    }
    else
    {
        echo 'No footer set';
    }
    echo "</div>";
    echo '</td>';
    echo '</tr>';
    echo '</table>';
?>

</body>
</html>
<?php
$myCon->DisconnectDB();
unset($myCon);
?>
